﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MyLibary.BIZ;

namespace MyLibary.DAL
{
    class DAL_HoaDonNhap
    {
        private static DAL_Connection connect = new DAL_Connection();
        #region Metholds
        internal static List<BIZ_HoaDonNhap> getAll()
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM HoaDonNhap");
            List<BIZ_HoaDonNhap> result = new List<BIZ_HoaDonNhap>();
            for(int i=0;i<tb.Rows.Count;i++)
                result.Add(new BIZ_HoaDonNhap(tb.Rows[i][0].ToString(),Convert.ToDateTime(tb.Rows[i][1].ToString()),Convert.ToDecimal(tb.Rows[i][2].ToString())));
            return result;
        }
        internal static BIZ_HoaDonNhap GetByID(string MaHDNhap)
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM HoaDonNhap WHERE MaHDNhap='" + MaHDNhap + "'");
            if (tb.Rows.Count < 1)
                return null;
            return new BIZ_HoaDonNhap(tb.Rows[0][0].ToString(), Convert.ToDateTime(tb.Rows[0][1].ToString()),Convert.ToDecimal(tb.Rows[0][2].ToString()));
        }
        internal static bool Insert(BIZ_HoaDonNhap HDNhap)
        {
            string formatDate = String.Format("{0:u}", HDNhap.NgayNhap);
            formatDate = formatDate.Substring(0, formatDate.Length - 1);
            string query = String.Format("INSERT INTO HoaDonNhap VALUES('{0}','{1}','{2}')", HDNhap.MaHDNhap, formatDate, HDNhap.TongTien);
            Console.WriteLine(query);
            return connect.ExecuteUpdate(query);
        }
        internal static bool Update(BIZ_HoaDonNhap HDNhap)
        {
            string formatDate = String.Format("{0:u}", HDNhap.NgayNhap);
            formatDate = formatDate.Substring(0, formatDate.Length - 1);
            Console.WriteLine(formatDate);
            string query = String.Format("UPDATE HoaDonNhap SET NgayNhap='{0}',TongTien='{1}' WHERE MaHDNhap='{2}'", formatDate, HDNhap.TongTien, HDNhap.MaHDNhap);
            Console.WriteLine(query);
            return connect.ExecuteUpdate(query);
        }
        internal static bool Delete(BIZ_HoaDonNhap HDNhap)
        {
            return connect.ExecuteUpdate("DELETE HoaDonNhap WHERE MaHDNhap='" + HDNhap.MaHDNhap + "'");
        }
        #endregion
    }
}
